package dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;


import bean.User;
import dao.UserDao;

@Repository
public class UserDaoImpl implements UserDao {
	
	//作用是注入“由unitName管理的EntityManager对象”
	//unitName就是EntityManagerFactoryBean的名字（可以由setPersistenceUnitName来设定）
	@PersistenceContext(unitName = "entityManagerFactory")
	private EntityManager entityManger;

	public UserDaoImpl() {
		System.out.println("userdao constructing....");
	}
	public int addUser(User u) {
		int rs = 0;
		entityManger.persist(u);
		rs=1;
		return rs;
	}
	public List<User> findAllUsers(){
		String jpql="from User";
		List<User> list=entityManger.createQuery(jpql).getResultList();
		return list;
	}

}
